import 'package:get/get.dart';
import 'package:ride_rex/main/home/landmark_bean.dart';
import 'package:ride_rex/net/common_net.dart';
import 'package:ride_rex/sqlite/db_manager.dart';
import 'package:ylm_ui_kit/ylm_ui_kit.dart';

import '../../net/common_global_url.dart';

class ApiHome extends CommonApi {
  ApiHome.unlockLandmark({required int outlay})
      : super(
          CommonGlobalUrl.consumeCoin,
          params: {'outlay': outlay, 'source': 'iap'},
        );
}

class HomeLogic extends GetxController with CommonLife {
  List<LandmarkBean> list = [];
  int unlockCount = 0;

  static const String keyListener = 'key_listener_unlock_landmark';

  void init() async {
    list = await DbManager.getLandmarkData();
    unlockCount = list.where((element) => element.isUnlock).length;
    update();
  }

  @override
  void onReady() {
    super.onReady();
    CommonObservable().addObserver(keyListener, unlock);
  }

  void unlock(key, [value]) async {
    LandmarkBean bean = value;
    await DbManager.updateLandmark(bean);
    init();
  }

  @override
  void onClose() {
    super.onClose();
    CommonObservable().removeObserver(keyListener, unlock);
  }

  @override
  void pageBack() {
    super.pageBack();
  }
}
